import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 引入
import Prism from "prismjs"; // 引入插件
import 'prismjs/themes/prism.css'; // 引入样式
import axios from 'axios'
Vue.config.productionTip = false
import VueCodemirror from 'vue-codemirror'

// require styles
import 'codemirror/lib/codemirror.css'
// require more codemirror resource...
import ElementUI, { Loading, Message } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
// you can set default global options and events when use
Vue.use(VueCodemirror, /* { 
  options: { theme: 'base16-dark', ... },
  events: ['scroll', ...]
} */)
import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

import VueCookies from 'vue-cookies'
VueCookies.config("60*60*3")


// const httpUrl = 'http://47.113.150.63/api/'
const httpUrl = '/api'
// axios.defaults.baseURL = '/api'
axios.defaults.baseURL = httpUrl



axios.interceptors.request.use(config => {
  console.log(config);
  if(config.method==='GET'||config.method==='get'){
    store.commit("openLoading")
  }
  config.headers.Authorization = VueCookies.get('token')
  console.log("==================");
  // console.log(config);
  return config
})
axios.interceptors.response.use(res => {
  // console.log(res);

  if(res.data.code === 2002){
    localStorage.clear()
    router.push('/401')
    VueCookies.remove("token");
  }
 
  if (res.data.code === 2011) {
    localStorage.clear()
    VueCookies.remove("token");
    Message.error(res.data.msg)
  }
  if (res.data.code === 2010) {
    const time = res.data.time
    if (time.startTime > time.currentTime) {
      console.log("time", time);
      const loading = Loading.service({           // 声明一个loading对象
        lock: true,                             // 是否锁屏
        text: `考试将在[${Math.floor((time.startTime - time.currentTime) / 1000)}]秒后开始，请稍后`,                         // 加载动画的文字
        // spinner: 'el-icon-loading',             // 引入的loading图标
        background: 'rgba(0, 0, 0, 0.8)',       // 背景颜色
        target: '.el-table, .table-flex, .region',       // **需要遮罩的区域，这里写要添加loading的选择器**
        fullscreen: false,
        customClass: 'loadingclass'             // **遮罩层新增类名，如果需要修改loading的样式**
      })
      const timer = setInterval(() => {
        loading.setText(`考试将在[${Math.floor((time.startTime - new Date().getTime()) / 1000)}]秒后开始，请稍后`);
      }, 1000)
      setTimeout(function () {                  // 设定定时器，超时5S后自动关闭遮罩层，避免请求失败时，遮罩层一直存在的问题
        loading.close();
        clearInterval(timer)                        // 关闭遮罩层
        router.go(0)
      }, time.startTime - time.currentTime)
    }
  }
  store.commit("closeLoading")
  return res
})
Vue.prototype.$http = axios
Vue.config.productionTip = false
Vue.prototype.$httpUrl = httpUrl
// VueCookies.set("token", "MTIz");
Vue.use(VueCookies)



new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
